package com.zf.yichat.api.controller.plantTree;


import com.zf.yichat.api.controller.BaseController;
import com.zf.yichat.api.dto.request.PlantTreeRequest;
import com.zf.yichat.mapper.UserPrintTreeConfigMapper;
import com.zf.yichat.model.SysDict;
import com.zf.yichat.model.UserPrintTreeConfig;
import com.zf.yichat.model.UserPrintTreeTask;
import com.zf.yichat.service.PlantTreeService;
import com.zf.yichat.service.SysDictService;
import com.zf.yichat.service.UserIntegralService;
import com.zf.yichat.utils.response.FsPage;
import com.zf.yichat.utils.response.FsResponse;
import com.zf.yichat.utils.response.FsResponseGen;
import com.zf.yichat.vo.DictKey;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 种树游戏
 */
@RestController
@RequestMapping("plantTree")
@Slf4j
public class PlantTreeController extends BaseController {


    @Autowired
    private PlantTreeService plantTreeService;
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private UserIntegralService userIntegralService;

    @Resource
    private UserPrintTreeConfigMapper printTreeConfigMapper;

    /**
     * 手动增加浇水次数
     * @return
     */
    @PostMapping("addUserPlantTreeTimes")
    public FsResponse addUserPlantTreeTimes() {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        UserPrintTreeConfig config = plantTreeService.queryUserPlantTreeConfig(userId);
        config.setPrintNum(100);
        printTreeConfigMapper.updateByPrimaryKeySelective(config);

        return FsResponseGen.successData(config);
    }


    /**
     * 获取能量块详情列表
     */
    @PostMapping("userEnergyBlockDetails")
    public FsResponse userEnergyBlockDetails(@RequestBody FsPage params) {
        Long userId = getUserId();

        return userIntegralService.userEnergyBlockDetails(FsPage.init(params.getPageNo(), params.getPageSize()), userId);
    }

    /**
     * 获取用户游戏初始化配置
     * @return
     */
    @PostMapping("queryUserPlantTreeConfig")
    public FsResponse queryUserPlantTreeConfig() {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }
        UserPrintTreeConfig config = plantTreeService.queryUserPlantTreeConfig(userId);
        return FsResponseGen.successData(config);
    }


    /**
     * 获取游戏任务列表
     * @return
     */
    @PostMapping("queryPlantTreeTask")
    public FsResponse queryPlantTreeTask() {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }
        List<UserPrintTreeTask> list = plantTreeService.queryPlantTreeTask(userId);
        return FsResponseGen.successData(list);
    }

    /**
     * 游戏任务列表-领取浇水次数
     * @return
     */
    @PostMapping("receivePlantTreeTask")
    public FsResponse receivePlantTreeTask(@RequestBody PlantTreeRequest request) {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        Long taskId = request.getTaskId();
        if (null == taskId) {
            return FsResponseGen.failMsg("参数错误【taskId】");
        }
        return plantTreeService.receivePlantTreeTask(userId, taskId);
    }

    /**
     * 游戏任务列表-使用浇水次数
     * @return
     */
    @PostMapping("usePlantTreeTask")
    public FsResponse usePlantTreeTask() {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        return plantTreeService.usePlantTreeTask(userId);
    }

    /**
     * 游戏任务列表-查询已浇水次数
     * @return
     */
    @PostMapping("queryPlantTreeInfo")
    public FsResponse queryPlantTreeInfo() {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        return FsResponseGen.successData(plantTreeService.queryPlantTreeInfo(userId));
    }

    /**
     * 游戏任务列表-查询未领取的能量块
     * @return
     */
    @PostMapping("queryUnclaimedEnergyBlock")
    public FsResponse queryUnclaimedEnergyBlock() {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        return plantTreeService.queryUnclaimedEnergyBlock(userId);
    }

    /**
     * 游戏任务列表-领取能量块接口
     * @return
     */
    @PostMapping("receiveEnergyBlock")
    public FsResponse receiveEnergyBlock(@RequestBody PlantTreeRequest request) {

        Long userId = getUserId();
        if (null == userId) {
            return FsResponseGen.failMsg("参数错误【userId】");
        }

        Long energyBlockId = request.getEnergyBlockId();
        if (null == energyBlockId) {
            return FsResponseGen.failMsg("参数错误【energyBlockId】");
        }
        return plantTreeService.receiveEnergyBlock(userId, energyBlockId);
    }


    /**
     * 获取抽奖规则
     * @return
     */
    @PostMapping("queryPlantTreeRule")
    public FsResponse queryPlantTreeRule() {

        SysDict dict = sysDictService.selectOne(DictKey.sys_dict_plant_tree_role);
        if (null == dict) {
            return FsResponseGen.successData("暂无配置！");
        }
        return FsResponseGen.successData(dict.getMemo());
    }


    /**
     * 获取抽奖公告
     * @return
     */
    @PostMapping("queryPlantTreeNotice")
    public FsResponse queryPlantTreeNotice() {

        SysDict dict = sysDictService.selectOne(DictKey.sys_dict_plant_tree_notice);
        if (null == dict) {
            return FsResponseGen.successData("暂无配置！");
        }
        return FsResponseGen.successData(dict.getMemo());
    }

}
